package redecolaborativa.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class PaginaLoginCadastro implements EntryPoint {
	private final FachadaAsync fachada = GWT.create(Fachada.class);	
	private final RootPanel rootPanel = RootPanel.get();
	
	public void onModuleLoad() {
		rootPanel.setSize("1100px", "400px");
		Image image = new Image("backgroundLoginCadastro.png");
		rootPanel.add(image, -38, 61);
		image.setSize("930px", "544px");
		cadastraUsuario();
		login();
	}
	
	private void cadastraUsuario() {
		CaptionPanel cptnpnlNewPanel = new CaptionPanel("Ainda n\u00E3o \u00E9 cadastrado?");
		cptnpnlNewPanel.setStyleName("caption-panel");
		cptnpnlNewPanel.setSize("339px", "460px");
		rootPanel.add(cptnpnlNewPanel, 916, 126);
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		absolutePanel.setSize("318px", "440px");
		cptnpnlNewPanel.setContentWidget(absolutePanel);
		
		final TextArea nome = new TextArea();
		nome.setSize("202px", "22px");
		absolutePanel.add(nome, 96, 91);
				
		final TextArea endereco = new TextArea();
		absolutePanel.add(endereco, 96, 150);
		endereco.setSize("202px", "103px");
		
		final TextBox loginCadastro = new TextBox();
		loginCadastro.setSize("202px", "20px");
		absolutePanel.add(loginCadastro, 96, 287);
		
		final TextBox senhaCadastro = new TextBox();
		senhaCadastro.setSize("202px", "20px");
		absolutePanel.add(senhaCadastro, 96, 343);
		
		Button btnCadastrar = new Button("Cadastrar");
		btnCadastrar.setSize("151px", "28px");
		absolutePanel.add(btnCadastrar, 128, 402);

		btnCadastrar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				try {
					fachada.cadastrarUsuario(nome.getText(), endereco.getText(), loginCadastro.getText(), senhaCadastro.getText(), 
					new AsyncCallback<Void>() {
						@Override
						public void onSuccess(Void result) {
							Window.alert("Cadastro realizado com sucesso!");
							limparCampos(nome, endereco, loginCadastro, senhaCadastro);
						}						
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Cadastro realizado com sucesso!");
							limparCampos(nome, endereco, loginCadastro, senhaCadastro);
						}
					});
				} catch (Exception e) {
					Window.alert(e.getMessage());
				}
			}
		});
		Label lblFaaJO = new Label("Fa\u00E7a j\u00E1 o seu cadastro!");
		lblFaaJO.setStyleName("labelCadastro");
		absolutePanel.add(lblFaaJO, 37, 24);
		lblFaaJO.setSize("275px", "32px");
		Label lblNome = new Label("Nome:");
		absolutePanel.add(lblNome, 38, 91);
		Label lblEndereco = new Label("Endereco:");
		absolutePanel.add(lblEndereco, 10, 152);
		Label lblLogin_1 = new Label("Login:");
		absolutePanel.add(lblLogin_1, 38, 287); 
		Label lblSenha_1 = new Label("Senha:");
		absolutePanel.add(lblSenha_1, 34, 343);
	}
	
	private void login() {
		final TextBox loginEntrar = new TextBox();
		rootPanel.add(loginEntrar, 821, 22);
		loginEntrar.setSize("128px", "20px");
		
		final PasswordTextBox passwordTextBox = new PasswordTextBox();
		rootPanel.add(passwordTextBox, 1032, 22);
		passwordTextBox.setSize("143px", "20px");
		
		Button btnEntrar = new Button("Entrar");
		rootPanel.add(btnEntrar, 1205, 22);
		btnEntrar.setSize("70px", "32px");
		btnEntrar.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
					try {
						fachada.abrirSessao(loginEntrar.getText(), passwordTextBox.getText(), 
						new AsyncCallback<String>() {
							@Override
							public void onSuccess(String result) {
								rootPanel.clear();
								PaginaInicial paginaInicial = new PaginaInicial(fachada, loginEntrar.getText(), result);
								paginaInicial.onModuleLoad();
							}						
							@Override
							public void onFailure(Throwable caught) {
								Window.alert(caught.getMessage());
							}
						});
					} catch (Exception e) {
						Window.alert(e.getMessage());
					}
			}
		});
		Label lblLogin = new Label("Login:");
		rootPanel.add(lblLogin, 772, 35);
		Label lblSenha = new Label("Senha:");
		rootPanel.add(lblSenha, 977, 35);
	}
	
	private void limparCampos(TextArea nome, TextArea endereco, TextBox loginCadastro, TextBox senha){
		nome.setText("");
		endereco.setText("");
		loginCadastro.setText("");
		senha.setText("");
	}	
}